package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.LunarUtil;

import java.util.List;

public class LunarMonth {
    private int year;
    private int month;
    private int dayCount;
    private int firstJulianDay;
    private int index;
    private int zhiIndex;

    public LunarMonth(int year, int month, int dayCount, int firstJulianDay, int index) {
        this.year = year;
        this.month = month;
        this.dayCount = dayCount;
        this.firstJulianDay = firstJulianDay;
        this.index = index;
        this.zhiIndex = (index-1+ LunarUtil.BASE_MONTH_ZHI_INDEX)%12;
    }

    @Override
    public String toString() {
        return year + "." + month;
    }


    public String toFullString()
    {
        return year + "年" + (isLeap() ? "闰" : "") + Math.abs(month) + "月(" + dayCount + "天)";
    }
    

    public static LunarMonth fromYm(int lunarYear, int lunarMonth)
    {
        return LunarYear.fromYear(lunarYear).getMonth(lunarMonth);
    }

    public int getYear()
    {
        return year;
    }

    public int getMonth()
    {
        return month;
    }

    public int getDayCount()
    {
        return dayCount;
    }

    public int getFirstJulianDay()
    {
        return firstJulianDay;
    }

    public int getIndex()
    {
        return index;
    }

    public int getZhiIndex()
    {
        return zhiIndex;
    }

    public int getGanIndex()
    {
        int offset = (LunarYear.fromYear(year).getGanIndex() + 1) % 5 * 2;
        return (index - 1 + offset) % 10;
    }

    public String getGan()
    {
        return LunarUtil.GAN.get(getGanIndex() + 1);
    }

    public String getZhi()
    {
        return LunarUtil.ZHI.get(getZhiIndex() + 1);
    }

    public String getGanZhi()
    {
        return getGan() + getZhi();
    }

    public String getPositionXi()
    {
        return LunarUtil.POSITION_XI.get(getGanIndex() + 1);
    }

    public String getPositionXiDesc()
    {
        return LunarUtil.POSITION_DESC.get(getPositionXi());
    }

    public String getPositionYangGui()
    {
        return LunarUtil.POSITION_YANG_GUI.get(getGanIndex() + 1);
    }

    public String getPositionYangGuiDesc()
    {
        return LunarUtil.POSITION_DESC.get(getPositionYangGui());
    }

    public String getPositionYinGui()
    {
        return LunarUtil.POSITION_YIN_GUI.get(getGanIndex() + 1);
    }

    public String getPositionYinGuiDesc()
    {
        return LunarUtil.POSITION_DESC.get(getPositionYinGui());
    }

    public String getPositionFu()
    {
        return getPositionFuBySect(2);
    }

    public String getPositionFuBySect(int sect)
    {
        List<String> fu = 1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2;
        return fu.get(getGanIndex() + 1);
    }

    public String getPositionFuDesc()
    {
        return getPositionFuDescBySect(2);
    }

    public String getPositionFuDescBySect(int sect)
    {
        return LunarUtil.POSITION_DESC.get(getPositionFuBySect(sect));
    }

    public String getPositionCai()
    {
        return LunarUtil.POSITION_CAI.get(getGanIndex() + 1);
    }

    public String getPositionCaiDesc()
    {
        return LunarUtil.POSITION_DESC.get(getPositionCai());
    }

    public boolean isLeap()
    {
        return month < 0;
    }

    public String getPositionTaiSui()
    {
        int m = Math.abs(month);
        String p;
        switch (m) {
            case 1:
            case 5:
            case 9:
                p = "艮";
                break;
            case 3:
            case 7:
            case 11:
                p = "坤";
                break;
            case 4:
            case 8:
            case 12:
                p = "巽";
                break;
            default:
                p = LunarUtil.POSITION_GAN.get(Solar.fromJulianDay(getFirstJulianDay()).getLunar().getMonthGanIndex());
        }
        return p;
    }

    public String getPositionTaiSuiDesc()
    {
        return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
    }

    public NineStar getNineStar()
    {
        index = LunarYear.fromYear(year).getZhiIndex() % 3;
        int m = Math.abs(month);
        int monthZhiIndex = (13 + m) % 12;
        int n = 27 - (index * 3);
        if (monthZhiIndex < LunarUtil.BASE_MONTH_ZHI_INDEX) {
            n -= 3;
        }
        int offset = (n - monthZhiIndex) % 9;
        return NineStar.fromIndex(offset);
    }

    public LunarMonth next(int n)
    {
        if (0 == n) {
            return LunarMonth.fromYm(year, month);
        } else {
            int rest = Math.abs(n);
            int ny = year;
            int iy = ny;
            int im = month;
            index = 0;
            List<LunarMonth> months = LunarYear.fromYear(ny).getMonths();
            if (n > 0) {
                while (true) {
                    int size = months.size();
                    for (int i = 0; i < size; i++) {
                        LunarMonth m = months.get(i);
                        if (m.getYear() == iy && m.getMonth() == im) {
                            index = i;
                            break;
                        }
                    }
                    int more = size - index - 1;
                    if (rest < more) {
                        break;
                    }
                    rest -= more;
                    LunarMonth lastMonth = months.get(size - 1);
                    iy = lastMonth.getYear();
                    im = lastMonth.getMonth();
                    ny++;
                    months = LunarYear.fromYear(ny).getMonths();
                }
                return months.get(index + rest);
            } else {
                while (true) {
                    int size = months.size();
                    for (int i = 0; i < size; i++) {
                        LunarMonth m = months.get(i);
                        if (m.getYear() == iy && m.getMonth() == im) {
                            index = i;
                            break;
                        }
                    }
                    if (rest <= index) {
                        break;
                    }
                    rest -= index;
                    LunarMonth firstMonth = months.get(0);
                    iy = firstMonth.getYear();
                    im = firstMonth.getMonth();
                    ny--;
                    months = LunarYear.fromYear(ny).getMonths();
                }
                return months.get(index - rest);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(LunarMonth.fromYm(2020, 1));
    }
}
